#
PKG = 'nfc_reader' # this package name
import roslib; roslib.load_manifest(PKG)

import rospy
from std_msgs.msg import String

import serial

ser=serial.Serial('/dev/ttyACM0')

def getData():
    return ord(ser.read())

def main():
	pub = rospy.Publisher('ndef', String)
    rospy.init_node('nfc_reader', anonymous=True)
	try:
	    while ser.isOpen():
			b=getData()
			while b==0: b=getData()
			    if(b==0xff):
			        size=getData()
				print "Size :",size
				csize=getData()
				if ((size+csize)&0xff)==0:
		            data=ser.read(size*8)
		        	if(getData()==0) : 
						print 'data:', data[3:len(data)]
						pub.publish(data)
					else :print 'ERROR'
				else: print 'ERROR'
	except:   
	    ser.close()
    
            
if __name__ == '__main__':
    listener()
